﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using SRB.Tg.Model;
using Teigha.Runtime;
using SRB.Tg.DrawingStandard;
using SRB.Tg.Cad_Bll;
using Teigha.Export_Import;
using System.Drawing;
using Teigha.Geometry;
using Teigha.GraphicsSystem;
using SRB.Tg.Cad_Bll;
using System.Collections.Specialized;
using System.IO;
using SRB.Tg.Operate;
using SRB.Tg.Common;
using SRB.Tg.Operate.Operate;

namespace ExportCushionStone
{
    public class ExportCushionStoneCAD
    {
        static string appPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "CADFiles";
        static System.Data.DataTable dtProject;
        static System.Data.DataTable dtProjectConfig;

        public static string DrawingCushionStoneSteel(string bridgeId, string catalogid,string _appPath = "")
        {
            string filePath = "";
            CushionStoneSteel P1 = new CushionStoneSteel(bridgeId, BridgeStructureType.AviationBridge, out dtProject);
            string file = "";
            List<string> fileList = new List<string>();
            if (!string.IsNullOrWhiteSpace(_appPath))
                appPath = _appPath;
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
            using (Services svc = new Services())
            {
                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                HostApplicationServices.Current = new HostAppServ(svc);
                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                using (Database db = new Database())
                {
                    HostApplicationServices.WorkingDatabase = db;

                    //db = new Database();
                    InitConfig.InitStandardConfig(db, bridgeId);
                    P1.DrawingPlinth(db, 10, bridgeId, out dtProjectConfig, catalogid);
                    drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtProject, catalogid, dtProjectConfig);
                    //LayoutManager LM = LayoutManager.Current;
                    //LM.CurrentLayout = "图纸";
                    if (db != null)
                    {
                        creatFolder(appPath + "\\CushionStoneReber");
                        //file = appPath + "\\AviationBridgeGA\\桥型图.dwg";
                        file = appPath + "\\CushionStoneReber\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + ".dwg"; 
                        db.SaveAs(file, DwgVersion.vAC18);

                    }
                }
            }
            using (Services ser = new Services())
            {
                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                HostApplicationServices.Current = new HostAppServ(ser);
                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                //打开数据库(dwg⽂件)
                using (Database database = new Database(false, false))//不加参数会出错
                {

                    database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                    LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                }
            }

            #region 合并pdf   
            FileInfo filepath = new FileInfo(file);
            string dir = filepath.DirectoryName + "\\PDF\\";
            string fileName = drawingConfigModel.drawing_no + " " + drawingConfigModel.name;
            filePath = dir + fileName + ".pdf";
            ITextPdfHelper.mergePDFFiles(fileList, filePath);
            #endregion

            if (!File.Exists(filePath))
                filePath = file.Replace(".dwg", "") + ".dwg";

            return filePath;
        }


        public static void creatFolder(String path)
        {
            //不存在 则创建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
    }
}
